<?php
class sqlTable {
	function __construct($module, $query, $currentPage=1, $rowsPerPage=DEFAULT_ROWS_PER_PAGE, $callbackFunctions=array(), $callbackParams=array()) {
		$query .= sql::getLimitSql($currentPage, $rowsPerPage);
		
		$data = $module->sql->getArrayFromDB($query);
		foreach ($module->sql->getFields() as $fieldNum => $field) {
			$module->xml->sqlTable->head->column[counter('sqlHeader')] = $field;
			
			if (isset($callbackFunctions[$fieldNum])) {
				if (!is_array(@$callbackParams[$fieldNum])) $callbackParams[$fieldNum] = array();
				foreach ($data as $k => $v) {
					$params = array_merge(array($data[$k][$field]), $callbackParams[$fieldNum]);
					$data[$k][$field] = call_user_func_array($callbackFunctions[$fieldNum], $params);
				}
			}
		}
		if (count($data)>0) {
			$i = 0;
			$j = 0;
			foreach ($data as $row) {
				$module->xml->sqlTable->row[$i] = "";
				foreach ($row as $cell) {
					$module->xml->sqlTable->row[$i]->cell[$j] = $cell;
					$j++;
				}
				$j = 0;
				$i++;
			}
		} else {
			$module->xml->sqlTable['empty'] = 1;
		}
		$module->engine->addTemplate(WIDGETS_TEMPLATES_PATH."sqlTable.xsl");
	}
}
?>